/**
 * fulihui.com Inc.
 * Copyright (c) 2015-2016 All Rights Reserved.
 */
package com.fulihui.information.core.repository.impl;

import com.fulihui.information.biz.conv.GoodFeedbackConv;
import com.fulihui.information.core.repository.GoodFeedbackRepository;
import com.fulihui.information.dal.dataobj.InfoGoodFeedbackRecord;
import com.fulihui.information.dal.dataobj.InfoGoodFeedbackRecordExample;
import com.fulihui.information.dal.mapper.InfoGoodFeedbackRecordMapper;
import com.fulihui.information.dto.InfoGoodFeedbackRecordDTO;
import com.fulihui.information.enums.GoodFeedbackStatusEnum;
import com.fulihui.information.request.GoodFeedbackQueryPageRequest;
import org.near.toolkit.common.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 有益反馈  Repository
 * @author zhangyingjie
 * @version $Id: v 0.1 2017年05月15日 15:07 zhangyingjie Exp $
 */
@Repository
public class GoodFeedbackRepositoryImpl implements GoodFeedbackRepository {
    @Autowired
    private InfoGoodFeedbackRecordMapper infoGoodFeedbackRecordMapper;
    @Override
    public boolean create(InfoGoodFeedbackRecordDTO dto) {
        int insert = infoGoodFeedbackRecordMapper.insert(GoodFeedbackConv.convertDTO2DO(dto));
        return insert>0;
    }

    @Override
    public InfoGoodFeedbackRecordDTO querySingle(Long id) {
        InfoGoodFeedbackRecord infoGoodFeedbackRecord = infoGoodFeedbackRecordMapper.selectByPrimaryKey(id.intValue());
        return GoodFeedbackConv.convertDO2DTO(infoGoodFeedbackRecord);
    }

    @Override
    public boolean update(InfoGoodFeedbackRecordDTO dto) {
        return infoGoodFeedbackRecordMapper.updateByPrimaryKeySelective(GoodFeedbackConv.convertDTO2DO(dto))>0;
    }

    @Override
    public List<InfoGoodFeedbackRecordDTO> queryPage(GoodFeedbackQueryPageRequest request) {
        InfoGoodFeedbackRecordExample example = new InfoGoodFeedbackRecordExample();
        InfoGoodFeedbackRecordExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(request.getPhone())){
            criteria.andPhoneLike(request.getPhone());
        }
        if (StringUtil.isNotBlank(request.getStatus())){
            criteria.andStatusEqualTo(request.getStatus());
        }
        example.setOffset(request.start4Mysql());
        example.setLimit(request.getRows());
        example.setOrderByClause("gmt_create desc");
        List<InfoGoodFeedbackRecord> infoGoodFeedbackRecords = infoGoodFeedbackRecordMapper.selectByExampleWithBLOBs(example);
        return GoodFeedbackConv.convertdo2dtoList(infoGoodFeedbackRecords);
    }

    @Override
    public int queryPageCount(GoodFeedbackQueryPageRequest request) {
        InfoGoodFeedbackRecordExample example = new InfoGoodFeedbackRecordExample();
        InfoGoodFeedbackRecordExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(request.getPhone())){
            criteria.andPhoneLike(request.getPhone());
        }
        if (StringUtil.isNotBlank(request.getStatus())){
            criteria.andStatusEqualTo(request.getStatus());
        }
        long count = infoGoodFeedbackRecordMapper.countByExample(example);
        return (int)count;
    }

    @Override
    public List<InfoGoodFeedbackRecordDTO> queryByIds(List<Integer> ids) {
        InfoGoodFeedbackRecordExample example = new InfoGoodFeedbackRecordExample();
        InfoGoodFeedbackRecordExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        //必须为未审核状态
        criteria.andStatusEqualTo(GoodFeedbackStatusEnum.PRE_AUDIT.getCode());
        List<InfoGoodFeedbackRecord> infoGoodFeedbackRecords = infoGoodFeedbackRecordMapper.selectByExampleWithBLOBs(example);
        return GoodFeedbackConv.convertdo2dtoList(infoGoodFeedbackRecords);
    }

    @Override
    public boolean batchUpdateByIds(List<Integer> ids, String status,String operator) {
        InfoGoodFeedbackRecordExample example = new InfoGoodFeedbackRecordExample();
        InfoGoodFeedbackRecordExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        //必须为未审核状态
        criteria.andStatusEqualTo(GoodFeedbackStatusEnum.PRE_AUDIT.getCode());
        InfoGoodFeedbackRecord record = new InfoGoodFeedbackRecord();
        record.setStatus(status);
        record.setGmtModified(new Date());
        record.setOperator(operator);
        return infoGoodFeedbackRecordMapper.updateByExampleSelective(record, example)>0;
    }
}
